package com.dlyk.service.impl;

import com.dlyk.constant.Constants;
import com.dlyk.mapper.TClueRemarkMapper;
import com.dlyk.model.TActivityRemark;
import com.dlyk.model.TClueRemark;
import com.dlyk.query.ClueRemarkQuery;
import com.dlyk.service.ClueRemarkService;
import com.dlyk.util.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/28 16:57:58
 * @version: 1.0
 */
@Service
public class ClueRemarkServiceImpl implements ClueRemarkService {

    @Resource
    private TClueRemarkMapper tClueRemarkMapper;

    @Override
    public int saveClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark = new TClueRemark();

        BeanUtils.copyProperties(clueRemarkQuery,tClueRemark);

        tClueRemark.setCreateTime(new Date());//创建时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(clueRemarkQuery.getToken());
        tClueRemark.setCreateBy(loginUserId);//创建人
        return tClueRemarkMapper.insertSelective(tClueRemark);
    }

    @Override
    public PageInfo<TClueRemark> getClueRemarkByPage(Integer current, ClueRemarkQuery clueRemarkQuery) {
        //设置PageHelper的分页参数
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TClueRemark> list = tClueRemarkMapper.selectClueRemarkByPage(clueRemarkQuery);
        //封装分页参数到PageInfo
        PageInfo<TClueRemark> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public TClueRemark getClueRemarkById(Integer id) {
        return tClueRemarkMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateClueRemark(ClueRemarkQuery clueRemarkQuery) {
        TClueRemark tClueRemark = new TClueRemark();

        BeanUtils.copyProperties(clueRemarkQuery,tClueRemark);

        tClueRemark.setEditTime(new Date());//编辑时间

        ///登陆人的id
        Integer loginUserId = JWTUtils.parseJWTByUserId(clueRemarkQuery.getToken());
        tClueRemark.setEditBy(loginUserId);//编辑人
        return tClueRemarkMapper.updateByPrimaryKeySelective(tClueRemark);
    }

    @Override
    public int delClueRemarkById(Integer id) {
        //逻辑删除
        TClueRemark tClueRemark = new TClueRemark();
        tClueRemark.setId(id);
        tClueRemark.setDeleted(1);//删除状态（null或者0正常，1删除）
        return tClueRemarkMapper.updateByPrimaryKeySelective(tClueRemark);
    }
}
